package com.ruoyi.file.service;

import com.alibaba.nacos.common.utils.IoUtils;
import com.ruoyi.file.utils.FileUploadUtils;
import com.ruoyi.file.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * qiniu 文件存储
 *
 * @author loong
 */
@Service("qiniu")
public class QiniuSysFileServiceImpl implements ISysFileService
{

    @Autowired
    private QiniuUtils qiniuUtils;

    /**
     * qiniu文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     */
    @Override
    public String uploadFile(MultipartFile file)
    {
        InputStream inputStream = null;
        try
        {
            String fileName = FileUploadUtils.extractFilename(file);
            inputStream = file.getInputStream();
            return qiniuUtils.upload(inputStream, fileName);
        }
        catch (Exception e)
        {
            throw new RuntimeException("Minio Failed to upload file", e);
        }
        finally
        {
            IoUtils.closeQuietly(inputStream);
        }
    }
}
